﻿using MX.Core.Caching.Memory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace UserCenter.Console.Sdk.Demo.Tools
{
    public static class HttpHelper
    {
        static MemoryCaching cache = new MemoryCaching("HttpHelper");

        public static T GetForm<T>(this HttpRequestMessage request, string key)
        {
            var json = cache.GetOrAdd(request.GetHashCode().ToString(), () =>
            {
               // var txt = request.Content.ReadAsAsync<string>().GetAwaiter().GetResult();
                return request.Content.ReadAsAsync<JObject>().GetAwaiter().GetResult();
            }, DateTime.Now.AddSeconds(20));
            if (json.TryGetValue(key, out JToken value))
            {
                return value.Value<T>();
            }
            return default(T);
        }
    }
}
